/*
 * tab_char_curve.cpp
 *
 *  Created on: 16.06.2013
 *      Author: SpirTanol
 */

#include "ui/tab_char_curve.h"
#include "ui/run_config_plot.h"
#include "ui/main_window.h"
#include "events/data_event.h"
#include "device/device_info.h"
#include "packets/run_config_packet.h"

// QT
#include <QGridLayout>
#include <QPushButton>
#include <QProgressBar>
#include <QLabel>
#include <QDateTime>
#include <QMessageBox>
#include <QDebug>
//======================================================================================
extern const QString cgProgressStyle;
//======================================================================================
TabCharCurve::TabCharCurve(MainWindow *mainWindow): oMainWindow(mainWindow)
{
	QGridLayout *l = new QGridLayout(this);

	oLabelTime = new QLabel(trUtf8("Время записи пусковой характеристики:"));
	l->addWidget(oLabelTime, 0, 0, 1, 3);

	l->addWidget(new QLabel(trUtf8("Полнота записанных данных")), 1, 0);

	oProgressCompleteness = new QProgressBar();
	oProgressCompleteness->setMaximum(65535);
	oProgressCompleteness->setStyleSheet(cgProgressStyle);
	oProgressCompleteness->setFormat("%p%");
	oProgressCompleteness->setValue(0);
	oProgressCompleteness->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
	l->addWidget(oProgressCompleteness, 1, 1);

	l->addWidget(new QLabel(trUtf8("Накопление данных")), 2, 0);

	oProgressAccum = new QProgressBar();
	oProgressAccum->setMaximum(65535);
	oProgressAccum->setStyleSheet(cgProgressStyle);
	oProgressAccum->setFormat("%p%");
	oProgressAccum->setValue(0);
	oProgressAccum->setAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
	l->addWidget(oProgressAccum, 2, 1);

	oBtnAutoSetup = new QPushButton(trUtf8("Автонастройка"));
	oBtnAutoSetup->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Expanding);
	oBtnAutoSetup->setEnabled(false);
	l->addWidget(oBtnAutoSetup, 1, 2, 2, 1);
	connect(oBtnAutoSetup, SIGNAL(clicked()), this, SLOT(onBtnAutoSetupClicked()));

	oBtnUpdatePlot = new QPushButton(trUtf8("Обновить график"));
	l->addWidget(oBtnUpdatePlot, 3, 2);
	connect(oBtnUpdatePlot, SIGNAL(clicked()), this, SLOT(onBtnUpdatePlotClicked()));

	oPlot = new RunConfigPlot();
	l->addWidget(oPlot, 4, 0, 1, 3);
	connect(oPlot, SIGNAL(buildProtectionGraphFinished()), this, SLOT(onBuildProtectionGraphFinished()));
}

TabCharCurve::~TabCharCurve()
{

}

void TabCharCurve::onDataEvent(const DataEvent *e)
{
	switch (e->type())
	{
	case DET_CONNECT:
		oPlot->setCurScale(oMainWindow->deviceInfo()->curScale());
		break;
	case DET_RUN_CONFIG_GET:
	{
		const RunConfigPacket *data = static_cast<const RunConfigEventGet*>(e)->runConfig();
		oProgressCompleteness->setValue(data->completeness());
		oLabelTime->setText(trUtf8("Время записи пусковой характеристики: ") +
				QDateTime::fromTime_t(data->time()).toString("dd.MM.yyyy hh:mm:ss"));

		QVector <double>currentDuration;
		for (int i = 0; i < 32; ++i)
			currentDuration.push_back(data->curDuration(i));
		oPlot->updateRunConfigData(currentDuration, data->curNom());
	}
		break;
	case DET_RUN_CONFIG_PROGRESS:
	{
		const RunConfigProgressPacket *data = static_cast<const RunConfigEventProgress*>(e)->progress();
		oBtnAutoSetup->setEnabled((data->state() != RunConfigProgressPacket::S_IN_PROCESS) &&
				(data->completenessPercent() >= 0.1));
		oProgressAccum->setValue(data->progress());
	}
		break;
	case DET_CONFIG:
		oPlot->updateConfigs(static_cast<const ConfigEvent*>(e)->config());
		onBtnUpdatePlotClicked();
		break;
	default: break;
	}
}
//======================================================================================
void TabCharCurve::onBtnUpdatePlotClicked()
{
	oBtnUpdatePlot->setText(trUtf8("Обновление графика..."));
	oBtnUpdatePlot->setEnabled(false);
	oPlot->buildProtectionGraph();
}

void TabCharCurve::onBuildProtectionGraphFinished()
{
	oBtnUpdatePlot->setText(trUtf8("Обновить график"));
	oBtnUpdatePlot->setEnabled(true);
}
//======================================================================================
void TabCharCurve::onBtnAutoSetupClicked()
{
	if (!oMainWindow->hasPassword())
	{
		QMessageBox::warning(this, trUtf8("Настройка"),
						trUtf8("Для этой операции необходимо ввести пароль."),
						QMessageBox::Ok, QMessageBox::Ok);
		oMainWindow->focusEditPassword();
		return;
	}

	oMainWindow->runAutoConfig();
}
//======================================================================================
void TabCharCurve::setScaleType(ScaleType scaleType)
{
	oPlot->setLogBottomScale(scaleType == ST_LOGO);
}
